﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace JipCrypt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string tempKey1, tempKey2;
        int tempInt;
        Random randomNumb = new Random();

        private void txtKey_TextChanged(object sender, EventArgs e)
        {
            if (txtKey.Text != "")
            {
                // 1 is for the end
                // 2 is for getting numbers
                tempKey1 = StringCrypt.CalculateMD5Hash(txtKey.Text);
                tempKey2 = StringCrypt.CalculateMD5Hash(txtKey.Text);
                for (int j = 0; j < tempKey2.Length; j++)
                {
                    if (!Char.IsDigit(tempKey2, j))
                    {
                        tempKey2 = tempKey2.Remove(j, 1);
                        j--;
                    }
                }
                tempKey2 = tempKey2.Remove(tempKey2.Length - ((9 - tempKey2.Length) * -1), (9 - tempKey2.Length) * -1);

                tempInt = Convert.ToInt32(tempKey2);
                while (tempInt >= 26)
                {
                    tempInt /= randomNumb.Next(1, 26);
                }

                while (true)
                {
                    if (Char.IsDigit(tempKey1, tempKey1.Length -1))
                    {
                        tempKey1 = tempKey1.Remove(tempKey1.Length - 1, 1);
                    }
                    else
                    {
                        break;
                    }
                }

                txtEncryptKey.Text = tempKey1 + tempInt.ToString();
            }
            else
            {
                txtEncryptKey.Text = "";
            }
        }

        private void txtPlainText_TextChanged(object sender, EventArgs e)
        {
            tempKey1 = StringCrypt.Vigenere(txtPlainText.Text, Regex.Replace(txtEncryptKey.Text, @"\d", ""));
            txtEncryptedText.Text = StringCrypt.Caesar(tempKey1, tempInt);
        }

        private void txtEncryptedText2_TextChanged(object sender, EventArgs e)
        {
            tempKey1 = StringCrypt.Caesar(txtEncryptedText2.Text, tempInt, true);
            txtPlainText2.Text = StringCrypt.Vigenere(txtEncryptedText2.Text, Regex.Replace(txtDecryptKey.Text, @"\d", ""), 94, true);
        }

        private void txtDecryptKey_TextChanged(object sender, EventArgs e)
        {
            tempKey1 = txtDecryptKey.Text;
            short i = 0;
            while (true)
            {
                if (Char.IsDigit(tempKey1, tempKey1.Length - (i + 1)))
                {
                    i += 1;
                }
                else
                {
                    tempInt = Convert.ToInt32(tempKey1.Remove(0, tempKey1.Length - i));
                    break;
                }
            }
        }        


    }
}
